package com.syg.dataource.controller;

import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author syg
 */
@RestController
public class ProgramTransactionController2 {


    @Resource
    PlatformTransactionManager transactionManager;

    /**
     * 编程式事务的第二种实现方式
     */
    @GetMapping("/programTransaction")
    public void testProgramTransaction() {
        TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
        try {
            // ....  业务代码
            transactionManager.commit(status);
        } catch (Exception e) {
            transactionManager.rollback(status);
        }
    }

}
